package com.wu.server.service;

import com.wu.server.code.Code;
import com.wu.server.dao.UserRepository;
import com.wu.server.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServer {
    @Autowired
    UserRepository userRepository;
    @Autowired
    Code code;
    public Object checkUser(String userName,String password,Boolean isManager){
        User user = userRepository.getUserInfoByUserName(userName);
        if (user != null){ //用户存在
            if (user.password.equals(password) && user.isManager == isManager) { // 正确验证争取
                HashMap<Object,Object> result = new HashMap<>();
                result.put("data",user);
                return code.getMeta(Code.SUCCESS,result);
            }else{ // 验证失败
                return code.getMeta(Code.checkFail,null);
            }
        }else{ //用户不存在
            return code.getMeta(Code.notUser,null);
        }
    }
    public Object saveUserInfo(User form){
        String userName = form.userName;
        User user = userRepository.getUserInfoByUserName(userName);
        if (user != null || form.userName.equals("") || form.password.equals("") || form.nickName.equals("") || form.isManager == null){
            return code.getMeta(Code.creatFail,null);
        }else{
            User userInfo = new User();
            userInfo.nickName = form.nickName;
            userInfo.password = form.password;
            userInfo.userName = form.userName;
            userInfo.isManager = form.isManager;
            userInfo.active = true;
            userRepository.save(userInfo);
            HashMap<Object,Object> result = new HashMap<>();
            result.put("data",userRepository.getUserInfoByUserName(userName));
            return code.getMeta(Code.SUCCESS,result);
        }
    }
    public Object updateUserInfo(User form){
        if (form.userName.equals("") || form.password.equals("") || form.nickName.equals("")){
            return code.getMeta(Code.creatFail,null);
        }else{
            User userInfo = new User();
            userInfo.id = form.id;
            userInfo.nickName = form.nickName;
            userInfo.password = form.password;
            userInfo.userName = form.userName;
            userInfo.isManager = form.isManager;
            userInfo.active = form.active;
            userRepository.save(userInfo);
            HashMap<Object,Object> result = new HashMap<>();
            result.put("data",userRepository.getUserInfoByUserName(form.userName));
            return code.getMeta(Code.SUCCESS,result);
        }
    }
    public Object getUserList(String id, String nickName, String sort, String order, int pageNum, int pageSize) {
        String idQuery = !id.equals("") ? id : null;
        String nickNameQuery = "%" + nickName + "%";
        Pageable pageable = PageRequest.of(pageNum - 1,pageSize, Sort.Direction.fromString(order),sort);
        Page<User> user = userRepository.queryUserBy(idQuery,nickNameQuery,pageable);
        List<User> data = user.getContent();
        HashMap<Object,Object> result = new HashMap<>();
        HashMap<String,Object> paging = new HashMap<>();
        paging.put("total",user.getTotalElements());
        paging.put("pageNum",user.getNumber()+1);
        paging.put("pageSize",user.getSize());
        result.put("paging",paging);
        result.put("data",data);
        return code.getMeta(Code.SUCCESS,result);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public Object deleteUserInfo(String id) {
        try{
            userRepository.deleteUserById(id);
            return code.getMeta(Code.SUCCESS,null);
        }catch (Exception e){
            return code.getMeta(Code.deleteFail,null);
        }
    }
}
